package com.novunda.license.core;

import java.util.Date;

/**
 * @author linfeng
 * @since 2016/10/12.
 */
class BaseLicenseValidate implements LicenseValidate {

    private static final String EXC_INVALID_SUBJECT = "错误的subject";
    private static final String EXC_LICENSE_IS_NOT_YET_VALID = "证书有效期在规定之前";
    private static final String EXC_LICENSE_HAS_EXPIRED = "证书已经失效";
    private static final String EXC_CONSUMER_TYPE_IS_NULL = "客户类型为空";
    private static final String EXC_CONSUMER_AMOUNT_IS_NOT_ONE = "客户数量小于1";

    @Override
    public void validate(LicenseContent license) throws LicenseException {

        if (license.getSubject() == null || license.getHolder() == null
                || license.getIssuer() == null || license.getIssued() == null) {
            throw new LicenseException(EXC_INVALID_SUBJECT);
        } else {
            Long var3 = new Date().getTime();
            Long var4 = license.getNotBefore();
            if (var4 != null && var3 <= var4) {
                throw new LicenseException(EXC_LICENSE_IS_NOT_YET_VALID);
            } else {
                Long var5 = license.getNotAfter();
                if (var5 != null && var3 >= var5) {
                    throw new LicenseException(EXC_LICENSE_HAS_EXPIRED);
                } else {
                    String var6 = license.getConsumerType();
                    if (var6 == null) {
                        throw new LicenseException(EXC_CONSUMER_TYPE_IS_NULL);
                    } else {

                        if (license.getConsumerAmount() < 1) {
                            throw new LicenseException(EXC_CONSUMER_AMOUNT_IS_NOT_ONE);
                        }

                    }
                }
            }
        }

    }


}
